package com.yanchangapp.ui.fragment.home;

import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.ui.activity._n_manage.CalendarRangeActivity;
import com.yanchangapp.ui.activity._n_manage.DailyPlanActivity;
import com.yanchangapp.ui.activity._n_manage.HistoryPlanActivity;
import com.yanchangapp.ui.activity._n_manage.LinkManActivity;
import com.yanchangapp.ui.activity._n_manage.OperateBookActivity;
import com.yanchangapp.ui.activity._n_manage.WorkItemActivity;
import com.zxl.baselib.baseapp.AppManager;
import com.zxl.baselib.ui.base.BaseFragment;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;
import com.yanchangapp.R;
import com.yanchangapp.model.cache.UserCache;
import com.yanchangapp.ui.activity.base.ChangePhoneActivity;
import com.yanchangapp.ui.activity.base.ChangePwdActivity;
import com.yanchangapp.ui.activity.base.LoginActivity;
import com.zxl.baselib.util.app.PackageHelper;
import com.zxl.baselib.util.ui.ResHelper;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MeFragment extends BaseFragment {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tvUserName)
    AppCompatTextView tvUserName;
    @BindView(R.id.tvMobile)
    AppCompatTextView tvMobile;
    @BindView(R.id.tvIdentify)
    AppCompatTextView tvIdentify;
    @BindView(R.id.tvPositionName)
    AppCompatTextView tvPositionName;
    @BindView(R.id.tvDepartmentName)
    AppCompatTextView tvDepartmentName;
    @BindView(R.id.tvCompanyName)
    AppCompatTextView tvCompanyName;
    @BindView(R.id.tvEmail)
    AppCompatTextView tvEmail;
    @BindView(R.id.llChangePhone)
    LinearLayout llChangePhone;
    @BindView(R.id.llChangePwd)
    LinearLayout llChangePwd;
    @BindView(R.id.btnLogoOut)
    Button btnLogoOut;
    @BindView(R.id.tvVersion)
    AppCompatTextView tvVersion;
    @Override
    public void init() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home_me;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void initView(View rootView) {
        ivToolbarNavigation.setVisibility(View.INVISIBLE);
        tvToolbarTitle.setText("个人中心");
        // 设置数据...
        tvUserName.setText(UserCache.getName());
        tvMobile.setText(UserCache.getMobile());
        tvIdentify.setText(UserCache.getIdentity());
        tvEmail.setText(UserCache.getUsername()+"@"+UserCache.getEmailDomain());
        tvPositionName.setText(UserCache.getPositionName());
        tvDepartmentName.setText(UserCache.getDepartmentName());
        tvCompanyName.setText(UserCache.getCompanyName());
        tvVersion.setText(PackageHelper.getAppVersion(getActivity()));
    }

    @Override
    public void initData() {
    }

    @Override
    public void initListener() {
        // 模拟进行检测...
        if (AppConstant.IS_TESTING){
            tvToolbarTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //WorkItemActivity.show(getActivity());
                    //OperateBookActivity.show(getActivity(),100);
                    //DailyPlanActivity.show(getActivity());
                    // LinkManActivity.show(getActivity());
                    // CalendarRangeActivity.show(getActivity());
                }
            });
        }
    }

    @OnClick({R.id.llChangePhone, R.id.llChangePwd, R.id.btnLogoOut})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llChangePhone:
                jumpToActivity(ChangePhoneActivity.class);
                break;
            case R.id.llChangePwd:
                jumpToActivity(ChangePwdActivity.class);
                break;
            case R.id.btnLogoOut:
                doLogout();
                break;
        }
    }

    public void doLogout(){
        Observable.create((ObservableOnSubscribe<Boolean>) emitter -> {
            UserCache.clear();
            emitter.onNext(true);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Boolean>() {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        AppManager.getAppManager().finishAllActivity();
                        jumpToActivityAndClearTask(LoginActivity.class);
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {

                    }
                });
    }
}
